1 /*
2 Copyright: Marcelo S. N. Mancini (Hipreme|MrcSnm), 2018 - 2021
3 License:   [https://creativecommons.org/licenses/by/4.0/|CC BY-4.0 License].
4 Authors: Marcelo S. N. Mancini
5 
6 	Copyright Marcelo S. N. Mancini 2018 - 2021.
7 Distributed under the CC BY-4.0 License.
8    (See accompanying file LICENSE.txt or copy at
9 	https://creativecommons.org/licenses/by/4.0/
10 */
11 module hip.global.gamedef;
12 import hip.systems.game;
13 import hip.config.opts;
14 import hip.api.data.font;
15 import hip.api.data.image;
16 
17 //Default assets
18 struct HipDefaultAssets
19 {
20    private __gshared IImage _texture;
21    private __gshared HipFont _font;
22 
23    // static const(IImage) texture(){return cast(const)_texture;}
24    // static const(IHipFont) font(){return cast(const)_font;}
25 
26    static IImage texture(){return _texture;}
27    static HipFont font(){return _font;}
28 
29    immutable static string textureData = import(HIP_DEFAULT_TEXTURE);
30    immutable static string fontData = import(HIP_DEFAULT_FONT);
31 }
32 
33 
34 public:
35    enum ENGINE_NAME = "Hipreme Engine";
36    enum int SCREEN_WIDTH = 800;
37    enum int SCREEN_HEIGHT = 600;
38    ///Globally shared for accessing it on Android Game Thread
39    __gshared GameSystem sys;
40 
41    __gshared float g_deltaTime = 0;
42 
43 
44 
45 
46 ///This function is callable only once.
47 bool loadDefaultAssets(void delegate() onSuccess, void delegate(string cause) onFailure)
48 {
49    import hip.font.ttf;
50    import hip.assets.image;
51    import hip.image_backend.impl;
52    import hip.console.log;
53    setImageDecoderProvider(&getDecoder);
54    __gshared int succeededSteps = 0;
55    enum ASSETS_TO_LOAD = 2;
56 
57    if(succeededSteps > 0)
58       return false;
59 
60 
61    hiplog("Loading default assets");
62 
63    auto image = new Image(HIP_DEFAULT_TEXTURE);
64    image.loadFromMemory(cast(ubyte[])HipDefaultAssets.textureData, (_)
65    {
66       hiplog("Loaded default image");
67       HipDefaultAssets._texture = image;
68       if(++succeededSteps == ASSETS_TO_LOAD)
69          onSuccess();
70    },
71    ()
72    {
73       onFailure("Failed loading default image.");
74    });
75 
76    hiplog("Loading default font");
77    auto font = new Hip_TTF_Font(HIP_DEFAULT_FONT, HIP_DEFAULT_FONT_SIZE);
78    if(!font.loadFromMemory(cast(ubyte[])HipDefaultAssets.fontData))
79       onFailure("Failed loading default font");
80    else
81    {
82       hiplog("Loaded default font");
83       HipDefaultAssets._font = font;
84       if(++succeededSteps == ASSETS_TO_LOAD)
85          onSuccess();
86    }
87 
88 
89    return true;
90 }
91 
92 pragma(inline, true)
93 {
94    import hip.event.dispatcher;
95    import hip.event.handlers.input_listener;
96    EventDispatcher HipInput()
97    {
98       return sys.dispatcher;
99    }
100    hip.event.handlers.input_listener.HipInputListener HipInputListener()
101    {
102       return sys.scriptInputListener;
103    }
104 }
105 
106 export extern(System)
107 {
108    import hip.api.input.core: IHipInput, IHipInputListener;
109    IHipInput HipInputAPI(){ return HipInput; }
110    IHipInputListener HipInputListenerAPI(){return HipInputListener;}
111    HipFont getDefaultFont()
112    {
113       return HipDefaultAssets.font;
114    }
115    /**
116    *  Use this instead of getDefaultFont.getFontWithSize, as it changes its internal state.
117    */
118    HipFont getDefaultFontWithSize(uint size)
119    {
120       return HipDefaultAssets._font.getFontWithSize(size);
121    }
122    IHipTexture getDefaultTexture()
123    {
124       __gshared IHipTexture texture;
125       if(texture is null)
126       {
127          import hip.assets.texture;
128          import hip.console.log;
129          logln("Image has loaded? ", HipDefaultAssets.texture.getName, HipDefaultAssets.texture.getWidth);
130          texture = new HipTexture(HipDefaultAssets.texture);
131       }
132       // return cast(const)texture;
133       return texture;
134    }
135 
136 
137 }